<template>
  <div class="page-box">
    <universal-table :columns="columns" :fetch-data="fetchTableData" permission-prefix="manager:profile" :show-selection="true"
      :show-index="true" :export-file-name="'协议'" :init-search-form="{}" @edit="handleEdit" @delete="handleDelete"
      @reset="reset" @status-change="handleStatusChange" ref="table">
      <!-- 搜索区域扩展 -->
      <template #searchExtend>
        <el-form-item prop="approved_status">
          <el-select v-model="searchForm.approved_status" placeholder="角色类型" clearable>
            <el-option v-for="item in approvedOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </template>
    </universal-table>

  </div>
</template>

<script>
  import UniversalTable from "@/components/TableComponent.vue";

  import {
    AGREEMENT_ROLE_TYPE,AGREEMENT_TYPE
  } from '@/constants/statusMap';
  export default {
    components: {
      UniversalTable

    },
    data() {
      return {
        AGREEMENT_ROLE_TYPE,
        AGREEMENT_TYPE,

        currentRow: null,
        columns: [{
            prop: "type",
            label: "角色类型",
            tag: true,
            tagConfig: AGREEMENT_ROLE_TYPE

          },
          {
            prop: "agreement_type",
            label: "协议类型",
            tag: true,
            tagConfig: AGREEMENT_TYPE
          },
          {
            prop: "title",
            label: "协议标题"
          },
          {
            prop: "create_time",
            label: "创建时间"
          }

        ],
        approvedOptions: [{
            id: 999,
            name: "全部"
          },
          {
            id: 1,
            name: "代理商"
          },
          {
            id: 2,
            name: "邻工"
          },
          {
            id: 3,
            name: "技工"
          },
          {
            id: 4,
            name: "管家"
          },
          {
            id: 5,
            name: "商家"
          },
        ],
        searchForm: {

        },
      };
    },
    methods: {
      // 获取表格数据
      async fetchTableData(params = {}) {
        const postData = Object.assign({}, params, this.searchForm);
        try {
          const result = await this.$httpRequest.post('/agreement/index', postData)
          console.log('-------fetchTableData----', result)
          return result
        } catch (e) {
          this.$message.error(e.message)
          throw e // 保持错误继续向上传递
        }

      },
      //重置
      reset() {
        this.searchForm.type = 999;

      },
      // 编辑
      handleEdit(row) {

        this.$router.push({
          path: '/config/agreement/agreement',
          query: {
            id: row.id,
          }
        });

      },

      // 删除
      async handleDelete(row) {
        console.log("删除:", row);
      },

      // 状态变更
      async handleStatusChange(row) {
        console.log("状态变更:", row);
        this.$message.success("状态更新成功");
      },
      handleApprove(payload) {
        console.log("审批信息：", payload);
        if (payload.action == 2 && payload.remark == '') {
          this.$message.error("拒绝通过需要备注原因")
          return false;
        }
        this.$httpRequest.post('/manager/approved', payload).then(result => {
          console.log('--approved---result------', result)
          if (result.code == 0) {
            this.$message.success('完成审核')
            this.$refs.table.refresh()
          } else {
            this.$message.error('审核失败')
          }
        }).catch(e => {
          this.$message.error(e.message)
        });
        this.dialogVisible = false;
      },
    },
  };
</script>

<style>
  .page-box {
    padding: 20px;

  }
</style>
